package com.study.dubbo.service.springpplication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lin
 * @create 2018-07-31 17:19
 **/
@Slf4j
public class DubboStartSpringApplication {
    private static final ReentrantLock LOCK = new ReentrantLock();
    private static final Condition STOP = LOCK.newCondition();

    public static ConfigurableApplicationContext run(Class<?> primarySource,
                                                     String... args) {
        return SpringApplication.run(new Class<?>[] { primarySource }, args);
    }

    public static void dubboRun(Class<?> primarySource,
                                String... args){

        run(primarySource,args);
        try {
            LOCK.lock();
            STOP.await();
        } catch (InterruptedException e) {
            log.warn("Dubbo service server stopped, interrupted by other thread!", e);
        } finally {
            LOCK.unlock();
        }
    }
}
